package org.dawnsql.stock;

import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

public class MyDownData {

    public static void getData()
    {
        String url = "https://stock.xueqiu.com/v5/stock/chart/kline.json?" +
                "symbol=SH601857" +
                "&begin=1691322224845" +
                "&period=day" +
                "&type=before" +
                "&count=-284" +
                "&indicator=kline,pe,pb,ps,pcf,market_capital,agt,ggt,balance";
        url = "https://stock.xueqiu.com/v5/stock/chart/kline.json?" +
                "symbol=SH601857" +
                "&begin=1691348363227" +
                "&period=day" +
                "&type=before&count=-284" +
                "&indicator=kline,pe,pb,ps,pcf,market_capital,agt,ggt,balance";
      String result = null;
        CloseableHttpClient httpclient = HttpClients.createDefault();
        try {
            // 创建httpget.
            HttpGet httpget = new HttpGet(url);
            httpget.addHeader("Content-Type", "text/html; charset=UTF-8");
            // 执行get请求.
            CloseableHttpResponse response = httpclient.execute(httpget);

            try {
                // 获取响应实体
                HttpEntity entity = response.getEntity();
                // 打印响应状态
                // //System.out.println(response.getStatusLine());
                if (entity != null) {
                    // 打印响应内容长度
                    // //System.out.println("Response content length: " +
                    // entity.getContentLength());
                    // 打印响应内容
                    result = EntityUtils.toString(entity, "utf-8").trim();
                    // //System.out.println("Response content: " + result);
                }
            } finally {
                response.close();
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭连接,释放资源
            try {
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println(result);
    }
}
